package com.tjec.project.mdmSynchronous.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.alibaba.druid.support.json.JSONUtils;
import com.tjec.common.constant.Constants;
import com.tjec.framework.redis.RedisCache;
import com.tjec.project.mdmSynchronous.api.ThreadPost;
import com.tjec.project.mdmSynchronous.service.MdmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tjec.project.mdmSynchronous.mapper.SysAdministrativeMapper;
import com.tjec.project.mdmSynchronous.domain.SysAdministrative;
import com.tjec.project.mdmSynchronous.service.ISysAdministrativeService;

/**
 * 行政组织架构Service业务层处理
 * 
 * @author villwang
 * @date 2020-05-13
 */
@Service
public class SysAdministrativeServiceImpl implements ISysAdministrativeService 
{
    @Autowired
    private SysAdministrativeMapper sysAdministrativeMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private MdmService mdmService;

    /**
     * 查询行政组织架构
     * 
     * @param guid 行政组织架构ID
     * @return 行政组织架构
     */
    @Override
    public SysAdministrative selectSysAdministrativeByGuid(String guid)
    {
        return sysAdministrativeMapper.selectSysAdministrativeByGuid(guid);
    }

    /**
     * 查询行政组织架构列表
     * 
     * @param sysAdministrative 行政组织架构
     * @return 行政组织架构
     */
    @Override
    public List<SysAdministrative> selectSysAdministrativeList(SysAdministrative sysAdministrative)
    {
        return sysAdministrativeMapper.selectSysAdministrativeList(sysAdministrative);
    }

    /**
     * 新增行政组织架构
     * 
     * @param sysAdministrative 行政组织架构
     * @return 结果
     */
    @Override
    public int insertSysAdministrative(SysAdministrative sysAdministrative)
    {
        return sysAdministrativeMapper.insertSysAdministrative(sysAdministrative);
    }

    /**
     * 修改行政组织架构
     * 
     * @param sysAdministrative 行政组织架构
     * @return 结果
     */
    @Override
    public int updateSysAdministrative(SysAdministrative sysAdministrative)
    {
        return sysAdministrativeMapper.updateSysAdministrative(sysAdministrative);
    }

    /**
     * 批量删除行政组织架构
     * 
     * @param guids 需要删除的行政组织架构ID
     * @return 结果
     */
    @Override
    public int deleteSysAdministrativeByGuids(String[] guids)
    {
        return sysAdministrativeMapper.deleteSysAdministrativeByGuids(guids);
    }


    /**
     * 删除行政组织架构信息
     * 
     * @param guid 行政组织架构ID
     * @return 结果
     */
    @Override
    public int deleteSysAdministrativeByGuId(String guid)
    {
        return sysAdministrativeMapper.deleteSysAdministrativeByGuid(guid);
    }


    /**
     *
     *@describe  行政组织架构
     *@params []
     *@return java.lang.Boolean
     *@author VillWang
     *@date 2020/5/13
     *@other
     *
     */
    @Override
    public Boolean regularlyUpdate() {

        //1.获取数据
        String url="${project.synchronous.http.sysAdministrative}";
        String token=mdmService.getMdmToken();
        Map<String, Object> requestString=ThreadPost.postRequestResult(url,null,token);
        List<SysAdministrative> list= dealWith(requestString);
        for (SysAdministrative sysAdministrative:list) {
            String globalId  = sysAdministrative.getGlobalId();
            //查看是否有该条数据
            SysAdministrative sat = sysAdministrativeMapper.selectSysAdministrativeByglobalId(globalId);

            //判断该数据是否是已存在 已存在更新，没有插入
            if(sat==null||sat.equals("")){
                //插入数据
                sysAdministrative.setGuid(UUID.randomUUID().toString());
                sysAdministrativeMapper.insertSysAdministrative(sysAdministrative);
            }else {
                //更新数据
                sysAdministrative.setGuid(sat.getGuid());

            }
            int res = sysAdministrativeMapper.updateSysAdministrative(sysAdministrative);

        }
        return true;
    }
    /**
     *
     *@describe  处理返回的json数据
     *@params []
     *@return java.util.List<com.tjec.project.mdmSynchronous.domain.SysAdministrative>
     *@author VillWang
     *@date 2020/5/14
     *@other
     *
     */
    public List<SysAdministrative> dealWith( Map<String, Object> map){
        List<SysAdministrative> sysAdministrativeArrayList=new ArrayList<SysAdministrative>();
        List<Map<String,Object>> list = (List<Map<String, Object>>) map.get("list");
        for (Map<String, Object> maps:list) {
            SysAdministrative sysAdministrative =  new SysAdministrative();
            sysAdministrative.setGlobalId((String) map.get("globalId"));
            sysAdministrative.setOaDeptId((String) map.get("oaDeptId"));
            sysAdministrative.setParentId((String) map.get("parentId"));
            sysAdministrative.setDeptName((String) map.get("deptName"));
            sysAdministrative.setDeptId((String) map.get("deptId"));
            sysAdministrative.setDeptFullName((String) map.get("deptFullName"));
            sysAdministrative.setOrderNum((String) map.get("orderNum"));
            sysAdministrativeArrayList.add(sysAdministrative);
        }
        return sysAdministrativeArrayList;
    }
}
